package com.chansos.dubbogo.project_generator;

import com.chansos.dubbogo.common.Registry;
import com.chansos.dubbogo.util.ResourceUtils;
import com.intellij.ide.util.projectWizard.ModuleWizardStep;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.SdkTypeId;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class GoModuleBuilder extends com.intellij.ide.util.projectWizard.ModuleBuilder {
    private GoModuleWizardStep moduleWizardStep;

    @Override
    public com.intellij.openapi.module.ModuleType<?> getModuleType() {
        return GoModuleType.INSTANCE;
    }

    @Override
    public boolean isSuitableSdkType(SdkTypeId sdkType) {
        return true;
    }

    @Override
    public void setupRootModel(@NotNull ModifiableRootModel modifiableRootModel) {
        ContentEntry contentEntry = doAddContentEntry(modifiableRootModel);
        if (contentEntry == null) {
            return;
        }
        @Nullable VirtualFile root = contentEntry.getFile();
        if (root == null) {
            return;
        }

        modifiableRootModel.inheritSdk();
        root.refresh(false, false);
        GoGenericTemplate template = moduleWizardStep.getSelection();
        String rootPath = root.getPath();

        Project project = modifiableRootModel.getProject();

        Map<String, String> commonArgs = getDefaultArgs(rootPath, project);
        ProjectGenerator generator = template.getGenerator();
        if (generator instanceof EmptyProjectGenerator) {
            ((EmptyProjectGenerator) generator).setComponents(moduleWizardStep.getComponents());
        }
        generator.generate(project, rootPath, commonArgs);

        ResourceUtils.init(rootPath);

        VfsUtil.markDirtyAndRefresh(false, true, true, root);
    }

    @Override
    protected void setupModule(Module module) throws ConfigurationException {
        super.setupModule(module);
    }

    @NotNull
    private Map<String, String> getDefaultArgs(String rootPath, Project project) {
        Map<String, String> commonArgs = new HashMap<>();
        commonArgs.put("${project-path}", rootPath);
        commonArgs.put("${project-name}", project.getName().replace(' ', '_'));
        Registry registry = moduleWizardStep.getRegistry();
        commonArgs.put("${registry-protocol}", registry.getAlias());
        commonArgs.put("${registry-address}", registry.getDefaultAddress());
        return commonArgs;
    }

    @Override
    public @Nullable ModuleWizardStep getCustomOptionsStep(WizardContext context, Disposable parentDisposable) {
        moduleWizardStep = new GoModuleWizardStep();
        return moduleWizardStep;
    }
}
